package com.cuitrip.business.home.travel;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.cuitrip.business.search.SearchActivity;
import com.cuitrip.service.R;
import com.cuitrip.util.track.TrackUtil;
import com.lab.app.BaseActivity;
import com.lab.app.customui.CustomUiConfig;
import com.lab.app.customui.CustomUiFragmentActivity;

/**
 * Created by ak
 */
public class TravelTripListActivity extends CustomUiFragmentActivity {

    public static final String KEY_WORD_KEY = "keyWord";
    public static final String CITY_KEY = "cityAbbr";
    public static final String FROM_KEY = "from";
    public static final String TITLE_KEY = "title";
    public static final String TAG_KEY = "tag";
    public static final String LANGUAGE_KEY = "language";
    public static final String SEARCH_PARAMS_KEY = "search_params";

    private String keyWords;
    private String cityAbbr;
    private String from;
    private String title;
    private String searchParams;
    private String searchTag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        if (getIntent()==null){
            super.onCreate(savedInstanceState, R.layout.page_container_layout);
            return;
        }   if (getIntent().hasExtra(KEY_WORD_KEY)) {
            keyWords = getIntent().getStringExtra(KEY_WORD_KEY);
            TrackUtil.trackScreenName("/search/results");
        }
        if (getIntent().hasExtra(CITY_KEY)) {
            cityAbbr = getIntent().getStringExtra(CITY_KEY);
            TrackUtil.trackScreenName("/%s/%s","TW",cityAbbr);
        }
        if (getIntent().hasExtra(FROM_KEY)) {
            from = getIntent().getStringExtra(FROM_KEY);
        }
        if (getIntent().hasExtra(TITLE_KEY)) {
            title = getIntent().getStringExtra(TITLE_KEY);
        }
        if (getIntent().hasExtra(SEARCH_PARAMS_KEY)){
            searchParams = getIntent().getStringExtra(SEARCH_PARAMS_KEY);
        }
        if (getIntent().hasExtra(SearchActivity.TAG)){
            searchTag = getIntent().getStringExtra(SearchActivity.TAG);
        }
        super.onCreate(savedInstanceState, R.layout.page_container_layout);
    }

    public void initView() {
        //TravelTripListFragment fragment = new TravelTripListFragment(keyWords,cityAbbr,from,title);
        TravelTripListFragment fragment = TravelTripListFragment.newInstance(keyWords,cityAbbr,from,title);
        Bundle args = fragment.getArguments();
        if (args == null) {
            args = new Bundle();
        }
        args.putString(SEARCH_PARAMS_KEY, searchParams);
        args.putBoolean("search", true);
        args.putString(SearchActivity.TAG,searchTag);
        Intent intent = getIntent();
        if (intent != null && intent.hasExtra(SOURCE_URI_KEY)) {
            args.putString(SOURCE_URI_KEY, intent.getStringExtra(SOURCE_URI_KEY));
        }
        fragment.setArguments(args);
        openFragment(fragment, R.id.container, TravelTripListFragment.class.getName());
    }


    @Override
    public boolean initCloseTopBar() {
        return true;
    }

    @Override
    public CustomUiConfig initCustomUiConfig() {
        return null;
    }

    public static void search(Activity activity, String title,String searchParams, String language,String uri) {
        Intent intent = new Intent(activity, TravelTripListActivity.class);
        if (!TextUtils.isEmpty(language)) {
            intent.putExtra(LANGUAGE_KEY, language);
        }
        if (!TextUtils.isEmpty(title)) {
            intent.putExtra(TITLE_KEY, title);
        }
        if (!TextUtils.isEmpty(searchParams)) {
            intent.putExtra(SEARCH_PARAMS_KEY, searchParams);
        }
        intent.putExtra(FROM_KEY, "app_card");
        JSONObject jsonObject = JSONObject.parseObject(searchParams);
        if (jsonObject.containsKey(CITY_KEY)) {
            String cityAbbr = jsonObject.getString(CITY_KEY);
            intent.putExtra(CITY_KEY, cityAbbr);
        }
        if (jsonObject.containsKey(KEY_WORD_KEY)) {
            String keyWord = jsonObject.getString(KEY_WORD_KEY);
            intent.putExtra(KEY_WORD_KEY, keyWord);
        }
        if(!TextUtils.isEmpty(uri)){
            intent.putExtra(SOURCE_URI_KEY,uri);
        }

        activity.startActivity(intent);
    }
}
